package inet.app.vod.activity.common;

import inet.app.vod.activity.R;
import inet.app.vod.activity.fun.IFunCategoryActivity;
import inet.app.vod.activity.news.INewsCategoryActivity;
import inet.app.vod.config.AppConstants;
import inet.app.vod.db.DaoCategory;
import inet.app.vod.db.DaoFactory;
import inet.app.vod.net.http.VolleyClient;
import inet.app.vod.net.http.VolleyListener;
import inet.app.vod.object.CategoryModel;
import inet.app.vod.object.ResultJsonModel;
import inet.app.vod.parser.Parse;
import inet.app.vod.util.MyUtil;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.inet.app.framework.intentutil.LaunchIntent;
import com.inet.app.framework.util.Log;
import com.inet.app.framework.util.text.StringUtil;

public class ICategoryActivity extends BaseActivity {
	
	public String mHomeId;
	
	private ListView mListView;
	private DaoCategory mDaoCategory;
	private ArrayAdapter<String> mCategoryAdapter;
	private List<CategoryModel> mCategories = new ArrayList<CategoryModel>();
	
	private void initModels() {
		mDaoCategory = DaoFactory.getInstance(getApplicationContext())
				.getDaoCategory();
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			mHomeId = bundle.getString("type");
		}
	}

	private void initViews() {
		mListView = (ListView) findViewById(R.id.type_list);
		mListView.setAdapter(mCategoryAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				CategoryModel category = mCategories.get(position);
				Bundle bundle = new Bundle();
				bundle.putSerializable("category", category);
				if (mHomeId.equals(AppConstants.ID_INEWS)) {
					LaunchIntent.start(ICategoryActivity.this,
							INewsCategoryActivity.class, bundle);
				} else {
					LaunchIntent.start(ICategoryActivity.this,
							IFunCategoryActivity.class, bundle);
				}
			}
		});
		if (mHomeId.equals(AppConstants.ID_INEWS)) {
			setTitle(AppConstants.NAME_INEWS);
		} else {
			setTitle(AppConstants.NAME_IFUN);
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_type);
		initUIBase();
		initModels();
		initViews();
		requestCategories();
	}

	public void requestCategories() {

		// get categories from database
		mCategories = mDaoCategory.selectCategoryFromGroup(mHomeId);
		if (mCategories != null && mCategories.size() > 0) {
			showCategories();
			return;
		}

		VolleyClient.requestCategories(mHomeId, new VolleyListener(this) {

			@Override
			public void onSuccess(String response) {
				Log.d("success request" + response);
				ResultJsonModel jResult = Parse.parseCategoryINews(response);
				if (jResult == null) {
					// error parser
					MyUtil.showDialog("Lỗi server!", ICategoryActivity.this);
				} else if (jResult.hasError()) {
					// error register
					String error = jResult.getError();
					MyUtil.showDialog(error, ICategoryActivity.this);
				} else {
					// Save categories to database
					mCategories = (List<CategoryModel>) jResult.getResult();
					saveCategoriesToDB();
					showCategories();
				}
			}
			@Override
			public void onStart() {
				Log.d("start request");
				showLoading(true);
			}

			@Override
			public void onFinish() {
				Log.d("finish request");
				showLoading(false);
			}

			@Override
			public void onError(VolleyError error) {
				MyUtil.showDialog("Lỗi server" + " (" + error.getMessage() + ")",
						ICategoryActivity.this);
			}
		});
	}

	/**
	 * Save categories of iNews to database
	 */
	private void saveCategoriesToDB() {
		if (mCategories == null) {
			return;
		}
		for (CategoryModel category : mCategories) {
			category.setId(StringUtil.generateUniqueString());
			if (TextUtils.isEmpty(category.getGroupId())) {
				category.setGroupId(mHomeId);
			}
			mDaoCategory.insertRow(category);
		}
	}

	/**
	 * Show categories in listview
	 */
	private void showCategories() {
		if (mCategories == null) {
			return;
		}
		List<String> categoryNames = new ArrayList<String>();
		for (CategoryModel category : mCategories) {
			categoryNames.add(category.getName());
		}
		mCategoryAdapter = new ArrayAdapter<String>(this,
				R.layout.item_listview_news, R.id.type_text, categoryNames);
		mListView.setAdapter(mCategoryAdapter);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void setTagMenu() {
		mTagMenu = mHomeId.equals(AppConstants.ID_INEWS) ? AppConstants.MENU_INEWS
				: AppConstants.MENU_IFUN;
	}

}
